"""
Sample requests:

curl -v http://127.0.0.1:8000/hello
curl -v http://127.0.0.1:8000/hello/Alexander
curl -v "http://127.0.0.1:8000/hello/Alexander?greeting=Hey"
curl -X POST -H "Content-Type: application/json" --data '{"username": "Alexander"}' http://127.0.0.1:8000/hello_post
curl -X POST -H "username: Alexander" http://127.0.0.1:8000/hello_header
curl -v http://127.0.0.1:8000/agent
curl -v http://127.0.0.1:8000/header/username/Alexander

Docs:
http://127.0.0.1:8000/docs
"""

from fastapi import Body, FastAPI, Header, Response

app = FastAPI()


@app.get("/hello")
@app.get("/hello/{username}")
def greet(username: str = None, greeting: str = None):
    """
    `greeting` is not on the path, so it will be treated as query parameter.
    """
    if not greeting:
        greeting = "Hello"

    if username:
        return f"{greeting} {username}!"

    return f"{greeting} FastAPI World!"


@app.post("/hello_post")
def greet_post(username: str = Body(embed=True)):
    """
    Docs for `Body(embed=True)`:
    https://fastapi.tiangolo.com/tutorial/body-multiple-params/?h=body+embed#embed-a-single-body-parameter
    """
    return f"Hello {username}!"


@app.post("/hello_header")
def greet_header(username: str = Header()):
    return f"Hello, {username}!"


@app.get("/agent", status_code=200)
def agent(user_agent: str = Header()):
    """
    FastAPI converts HTTP header keys to lowercase, and converts a hyphen (-) to an underscore (_).
    So you could print the value of the HTTP User-Agent header like this:
    """
    return user_agent


@app.get("/header/{name}/{value}")
def header(name: str, value: str, response: Response):
    """Create custom header"""
    response.headers[name] = value
    return "Normal body"


if __name__ == "__main__":
    import uvicorn

    uvicorn.run("hello:app", reload=True)
